package com.learning.javaweb_servlet_jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class JDBC_Teacher_Update {
    //设置基础数据，链接数据库
    static final String URL = "jdbc:mysql://localhost:3306/jdbc_demo?useSSL=false&serverTimezone=GMT&characterEncoding=UTF-8";
    static final String USER = "root";
    static final String PASSWORD = "WQ9ABCAT";

    //更新语句
    static final String SQL = "UPDATE teacher SET name = ? WHERE id = ?";

    public static void main(String[] args) {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);) {
            conn.setAutoCommit(false);
            try (PreparedStatement ps = conn.prepareStatement(SQL);) {
                // 设置参数
                ps.setString(1, "宝儿姐");
                ps.setInt(2, 1);
                // 执行插入或更新
                ps.executeUpdate();
                conn.commit();
            } catch (SQLException e) {
                conn.rollback();
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
